package com.whfc.wxmp.util;

import com.whfc.common.util.JSONUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

import java.util.Enumeration;
import java.util.Map;

import static org.springframework.web.context.request.RequestAttributes.SCOPE_REQUEST;

/**
 * @Description: 固定请求参数
 * @author: xugcheng
 * @version: 1.0
 * @date: 2020/1/8 10:03
 */
public class RequestConetxtUtil {

    public static final String APP_TYPE = "appType";
    public static final String PAGE = "page";

    private static Logger logger = LoggerFactory.getLogger("");

    /**
     * 设置键值对
     *
     * @param key
     * @param value
     */
    public static void set(String key, Object value) {
        RequestContextHolder.currentRequestAttributes().setAttribute(key, value, SCOPE_REQUEST);
    }

    /**
     * 获取值
     *
     * @param key
     * @return
     */
    public static Object get(String key) {
        return RequestContextHolder.currentRequestAttributes().getAttribute(key, SCOPE_REQUEST);
    }

    public static void setAppType(String appType) {
        set(APP_TYPE, appType);
    }

    public static String getAppType() {
        return String.valueOf(get(APP_TYPE));
    }

    public static void setPage(String page) {
        set(PAGE, page);
    }

    public static String getPage() {
        return String.valueOf(get(PAGE));
    }

    public static String getParameters() {
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        Map<String, String[]> parameterMap = request.getParameterMap();
        return JSONUtil.toString(parameterMap).replaceAll("\\[", "").replaceAll("\\]", "");
    }
}
